<?php

namespace app\common\model;

use think\Model;

/**
 * 会员余额日志模型
 */
class MoneyLog extends Model
{
    // 表名
    protected $name = 'user_money_log';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = '';
    // 追加属性
    protected $append = [
        'createtime_text',
        'goods_name_text',
        'phone_text'
    ];

    public function getCreateTimeTextAttr($value,$data)
    {
        if(isset($data['createtime'])){
            $value = date('Y-m-d H:i:s',$data['createtime']);
        }
        return $value;
    }

    public function getGoodsNameTextAttr($value,$data)
    {
        if(isset($data['goods_id']) && isset($data['memo'])){
            $goodsname = Packages::where(['id'=>$data['goods_id']])->value('user_name');
            $phone = '';
            if(isset($data['order_id'])){
                $phone = Order::where(['order_id'=>$data['order_id']])->value('phone');
            }
            if(!$goodsname){
                $value = '【结算失败追回佣金】';
            }else{
                $value = '【'.$goodsname.'】'.$phone;
            }


        }
        return $value;
    }
    public function getPhoneTextAttr($value,$data)
    {
        if(isset($data['order_id'])){
            $phone = Order::where(['id'=>$data['order_id']])->value('phone');
            $value = $phone;
        }
        return $value;
    }

}
